package com.example.test.controllers;

import com.example.test.dao.ActorRepository;
import com.example.test.dao.RoleRepository;
import com.example.test.entities.Actor;
import com.example.test.entities.Roles;
import com.example.test.services.ActorService;
import com.example.test.services.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("Roles")
public class RoleControllers {

    @Autowired
    RoleService roleService;
    @Autowired
    ActorService actorService;

    @GetMapping
    public String displayRoles(Model model){

        List<Roles> roles = roleService.getAll();
        model.addAttribute("roles",roles);
        return "roles/list-roles";
    }

    @GetMapping("/new")
    public String displayRoleForm(Model model){

        Roles aRole=new Roles();
        List<Actor> actors = actorService.getAll();
        model.addAttribute("roles",aRole);
        model.addAttribute("thisActor",actors);
        return "new-role";
    }

    @PostMapping("/save")
    public String creatRole(Model model, @RequestParam List<Long> actors, Roles role){
        roleService.save(role);

        return "redirect:/roles";
    }

}